<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 18/4/3
 * Time: 14:50
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');

class GroupController extends Controller
{
    public function init()
    {
        $this->authentication = $this->authentication();
        if(!$this->authentication)
        {
            $this->redirect('/');
            exit;
        }
        $this->group = new GroupModel();
        $this->getView()->assign("user", $this->authentication['data']);
        $group = $this->group->listsAction($this->authentication['data']);
        $this->getView()->assign("group", $group);
    }

    /**
     * 分类添加
     */
    public function addAction()
    {

        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'name' => 'chsDash|min:1|max:8',
                'status' => 'require|number|between:1,2',
            ]);
            $validate->message([
                'name.chsAlphaNum' => '分组名称只能是汉字、字母和数字',
                'name.min' => '分组名称不能小于2个字符',
                'name.max' => '分组名称不能大于8个字符',
                'status.require' => '分组状态不能为空',
                'status.number' => '分组状态类型错误',
                'status.between' => '分组状态不在范围内',
            ]);
            if ($validate->check($content)) {
                $data = $this->group->addAction($content,$this->authentication['data']);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }

    /**
     * 分类修改
     */
    public function editAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'name' => 'chsDash|min:1|max:8',
                'gid' => 'require|number|egt:1|exist:group,id',
                'status' => 'require|number|between:1,2',
            ]);
            $validate->message([
                'name.chsAlphaNum' => '分组名称只能是汉字、字母和数字',
                'name.min' => '分组名称不能小于2个字符',
                'name.max' => '分组名称不能大于8个字符',
                'gid.require' => '分组ID不能为空',
                'gid.number' => '分组ID只能是数字',
                'gid.egt' => '分组ID错误',
                'gid.exist' => '分组ID不存在',
                'status.require' => '分组状态不能为空',
                'status.number' => '分组状态类型错误',
                'status.between' => '分组状态不在范围内',
            ]);
            if ($validate->check($content)) {
                $data = $this->group->editAction($content);
                $code = 200;
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }
}